package com.main.ok.callback;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.common.util.concurrent.MoreExecutors;
import com.google.common.util.concurrent.RateLimiter;

public class TestCallable {

    public static void main(String[] args) throws InterruptedException, ExecutionException {
        ListeningExecutorService executorService = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(3));
        RateLimiter limit = RateLimiter.create(5.0);

        for (int i = 0; i < 1; i++) {
            limit.acquire();
            ListenableFuture<Integer> future = executorService.submit(new Task());
            Futures.addCallback(future, new FutureCallback<Integer>() {

                @Override
                public void onSuccess(Integer result) {
                    // TODO Auto-generated method stub
                    System.out.println(result);
                }

                @Override
                public void onFailure(Throwable t) {
                    // TODO Auto-generated method stub
                    System.out.println(t.getMessage());
                }
            });
        }

    }

    static class Task implements Callable<Integer> {

        public Integer call() throws Exception {
            // TODO Auto-generated method stub
            System.out.println(Thread.currentThread().getName());
            // TimeUnit.SECONDS.sleep(1);
            return 5;
        }

    }
}
